package com.sgb.kjwl.view.ui.activity.enterprise;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityReceiptBinding;
import com.sgb.kjwl.view.ui.adapter.enterprise.ReceiptAdpater;
import com.sgb.kjwl.viewmodel.enterprise.ReceiptViewModel;
import com.sgb.link.constant.RequestCode;
import com.sgb.link.router.PathUser;
import com.swgk.core.base.model.entity.InvoicingInfoEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.io.Serializable;

/**
 * 选择开票信息
 */
@Route(path = PathUser.ReceIptActivity)
public class ReceIptActivity extends AppActivity {

    private ActivityReceiptBinding mBinding;
    private ReceiptViewModel mViewModel;
    private ReceiptAdpater mAdpater;



    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(ReceiptViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_receipt);
        mBinding.setVariable(BR.viewmodel, mViewModel);
        mBinding.setLifecycleOwner(this);

        initRecyclerView();
        initObsever();
        initLister();


        mViewModel.getListdata(false);
    }


    private void initRecyclerView(){
        mAdpater = new ReceiptAdpater(this);
        mBinding.recycler.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recycler.setAdapter(mAdpater);
    }


    private void initObsever(){
        mViewModel.ToastShow.observe(this, data -> {
            MToast.showLongToast(this, data);
        });

        mViewModel.showloading.observe(this, type_int -> {
            switch (type_int) {
                case 1:
                    DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });
        mViewModel.listdata.observe(this,listdata->{
            if(listdata.size()<10){
                mBinding.recycler.setNoMore(true);
            }
            if (mViewModel.loadmore) {
                mBinding.recycler.loadMoreComplete();
                mAdpater.addDatas(listdata);

            } else {
                mBinding.recycler.refreshComplete();
                if (listdata.size() == 0) {
                    mBinding.recycler.setVisibility(View.GONE);
                } else {
                    mBinding.recycler.setVisibility(View.VISIBLE);
                    mAdpater.setDatas(listdata);
                }
            }
        });
    }


    private void initLister(){
        mAdpater.setOnItemClick(new AdapterOnItemClick<InvoicingInfoEntity>() {
            @Override
            public void onItemClick(InvoicingInfoEntity invoicingInfoEntity, int position) {
                Intent mintent = new Intent();
                mintent.putExtra("data",(Serializable)invoicingInfoEntity);
                setResult(RequestCode.CODE_ZERO,mintent);
                finish();

            }
        });
        mBinding.recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.getListdata(false);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getListdata(true);
            }
        });

    }




    public static void startactivity(Context mContext){
        Intent mIntent = new Intent(mContext,ReceIptActivity.class);
        mContext.startActivity(mIntent);
    }

    public static void startactivityForResult(Activity mContext,int requestcode){
        Intent mIntent = new Intent(mContext,ReceIptActivity.class);
        mContext.startActivityForResult(mIntent,requestcode);
    }
}
